﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using Coda.Nth.DbConnect;
using Coda.Nth.DAO;
using System.Data;
using System.Data.Objects;
using Coda.Nth.BusinessLogic;
using System.ComponentModel.Composition;


namespace NthParameterSeries.Web
{
    [ServiceContract(Namespace = "www.NthParameterSeries.Codeplex.com")]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PriceService
    {
        public PriceService()
        {
            BusinessFacade = new Coda.Nth.BusinessLogic.BusinessFacade();  
            //var catalog = new DeploymentCatalog();
            //var container = new CompositionContainer(catalog);
        }

        [Import("ExamplePart")]
        public IBusinessFacade BusinessFacade { get; set; }

        [OperationContract]
        public IEnumerable<Price> GetPrices()
        {
            return BusinessFacade.GetPrices();
            //using (var context = new NthDbConnectContainer())
            //{
            //    //var super = new Portfolio() { Id = 1,Name = "Super", Address = new Location() { City = "Tokio", Country = "Japan" }, InceptionDate = DateTime.Now};

            //    //    context.PriceSet.Add(new Price(){AsOfDate= DateTime.Now, 
            //    //        Nav=12m, Portfolio = super, Id = 2, EntryDate= DateTime.Now, Ror = 0.02m});
            //    //    context.PortfolioSet.Add(super);
            //    //    context.SaveChanges();

            //    var price = context.PriceSet.Select(x => x).ToList<Price>();
            //    return price;
            //}
        }

        [OperationContract]
        public IEnumerable<City> GetCities()
        {
            return BusinessFacade.GetCities();
            //using (var context = new NthDbConnectContainer())
            //{
            //    var cities = context.CitySet.Select(x => x).ToList();
            //    cities.ForEach(x => context.Detach(x));
            //    return cities;
            //}
        }

        [OperationContract]
        public IEnumerable<Country> GetCountries()
        {
            return BusinessFacade.GetCountries();
            //using (var context = new NthDbConnectContainer())
            //{

            //    var countries = context.CountrySet.Select(x => x).ToList();
            //    countries.ForEach(x => context.Detach(x));
            //    return countries;
            //}
        }

        [OperationContract]
        public IEnumerable<DaoManagerFee> GetManagerFees()
        {
            
            using (var context = new NthDbConnectContainer())
            {
                var fees = from manager in context.FeeSet.OfType<ManagerFee>()
                           select new DaoManagerFee { Amount = manager.Amount, Id = manager.Id, Manger = manager.Manger };


                return fees.ToList();
            }
        }

        //[OperationContract]
        //public IEnumerable<DaoPortfolio> GetPortfolios()
        //{
        //    var context = new NthDbConnectContainer();
        //    var pr = context.PortfolioSet.Select(x => new DaoPortfolio() { Name = x.Name, Id = x.Id, InceptionDate = x.InceptionDate, Address = new DaoAddress() { Country = x.Address.Country } }).ToList();
        //    return pr;
        //}

        //[OperationContract]
        //public void PutPortfolio(DaoPortfolio daoPortfolio)
        //{
        //    var context = new NthDbConnectContainer();
        //    var portfolio = new Portfolio() { InceptionDate = daoPortfolio.InceptionDate, Name = daoPortfolio.Name, Id = daoPortfolio.Id, Address = new Location() { City = daoPortfolio.Address.City, Country = daoPortfolio.Address.Country } };
        //    context.PortfolioSet.AddObject(portfolio);
        //    context.SaveChanges();
        //}

        //[OperationContract]
        //public IEnumerable<DaoPortfolio> GetPortfolio2(int Id)
        //{
        //    var context = new NthDbConnectContainer();
        //    var pr = context.PortfolioSet.Select(x => new DaoPortfolio() { Name = x.Name, Id = x.Id, InceptionDate = x.InceptionDate, Address = new DaoAddress() { Country = x.Address.Country } }).ToList();
        //    return pr;
        //}

        [OperationContract]
        public void PutPortfolios(IEnumerable<Portfolio> portfolios)
        {
            BusinessFacade.PutPortfolios(portfolios);
            //using (var context = new NthDbConnectContainer())
            //{
            //    portfolios.Where(x => x.Id != 0).ToList().ForEach(y =>
            //    {
            //        context.Attach(y);
            //        context.ObjectStateManager.ChangeObjectState(y, EntityState.Modified);
            //    });
            //    portfolios.Where(y => y.Id == 0).ToList().ForEach(x => context.AddToPortfolioSet(x));
            //    context.SaveChanges();
            //}
        }

        [OperationContract]
        public IEnumerable<Portfolio> GetPortfolios()
        {
            return BusinessFacade.GetPortfolios();
            //using (var context = new NthDbConnectContainer())
            //{
            //    var pr = context.PortfolioSet.Select(x => x).ToList();
            //    pr.ForEach(y => context.Detach(y));
            //    return pr;
            //}
        }

        //[OperationContract]
        //public IEnumerable<String> GetString()
        //{
        //    var context = new NthDbConnectContainer();
        //    var pr = context.PortfolioSet.Select(x => x.Name);
        //    return pr;

        //}

        
    }
}
